package com.thememaker.lockstyle.customview;

import com.thememaker.lockstyle.parsii.ExpressionUtils;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;

public class EditItemHBox extends HBox {

    private EditItemHBox(){}

    private Label leftLabel;
    private TextField textField;
    private long lastEditTime;
    private Timer timer;
    private boolean isError = false;

    public static EditItemHBox initEditItem(String name, String value,int prefWidth){
        EditItemHBox editItemHBox = new EditItemHBox();
        editItemHBox.setAlignment(Pos.CENTER_LEFT);
        editItemHBox.leftLabel = new Label(name + " : ");
        editItemHBox.leftLabel.setId("diy-fontColor");
        editItemHBox.textField = new TextField();
        editItemHBox.textField.setPrefWidth(prefWidth);
        editItemHBox.textField.setId("diy-textfield");
//        HBox.setHgrow(editItemHBox.textField, Priority.ALWAYS);
        editItemHBox.textField.setText(value);
        editItemHBox.getChildren().addAll(editItemHBox.leftLabel,editItemHBox.textField);
        return editItemHBox;
    }

    public void addEditCheck(ExpressionUtils expressionUtils,int checkTime){
        textField.textProperty().addListener((observable, oldValue, newValue) -> {
            lastEditTime = System.currentTimeMillis();
            if (timer!=null){
                timer.cancel();
            }
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (System.currentTimeMillis()-lastEditTime>=checkTime){
                        Platform.runLater(()->{
                            textField.setId(Optional.of(newValue.trim())
                                    .filter(str -> str.length() > 0 && expressionUtils.parseExpr(str) == -9999)
                                    .map(b -> {
                                        isError = true;
                                        return "textfield-error";
                                    })
                                    .orElseGet(()->{
                                        isError = false;
                                        return "diy-textfield";
                                    }));
                            timer.cancel();
//                            if (Strings.isNullOrEmpty(newValue.trim())){
//                                textField.setId("diy-textfield");
//                            }else{
//                                double parseVale = expressionUtils.parseExpr(newValue);
//                                if (parseVale != -9999){
//                                    textField.setId("diy-textfield");
//                                }else{
//                                    textField.setId("textfield-error");
//                                }
//                            }
                        });
                    }
                }
            }, checkTime);
        });
    }

    public String getEditValue(){
        return textField.getText();
    }

    public void setValue(String value){
            textField.setText(value);
    }

    public void setEditable(boolean editable){
        textField.setEditable(editable);
    }

    public boolean isError() {
        return isError;
    }

    public TextField getTextField() {
        return textField;
    }

    public void setTextField(TextField textField) {
        this.textField = textField;
    }
}
